﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class IDManager<T> where T: class
{
    int maxIndex;
    int startIndex;
    //T[] idArr;
    Dictionary<string, T> idDic;

    public IDManager(int maxIndex = 9999, int startIndex = 1)
    {
        this.maxIndex = maxIndex;
        this.startIndex = startIndex;
        //idArr = new T[maxIndex];
        idDic = new Dictionary<string, T>();
    }

    public void Record(string id, T obj, bool replace = true)
    {
        //int index;
        //if (int.TryParse(id, out index))
        //    Record(index, obj, replace);
        //else
        //    Debug.LogError("ID格式不正确!"+id+"-"+obj);

        if (replace)
        {
            idDic[id.ToString()] = obj;
        }
        else
        {
            idDic.AddRep(id, obj);
        }
    }

    public void Remove(int id)
    {
        //idArr[id] = null;
        if (idDic.ContainsKey(id.ToString()))
            idDic.Remove(id.ToString());
    }
    public void Remove(string id)
    {
        //int index;
        //if (int.TryParse(id, out index))
        //    idArr[index] = null;
        //else
        //    Debug.LogError("ID格式不正确!" + id);
        if (idDic.ContainsKey(id))
            idDic.Remove(id);
    }
    public void Remove(T obj)
    {
        //bool findFlag = false;
        //for (int i = 0; i < maxIndex; i++)
        //{
        //    if (idArr[i] == obj)
        //    {
        //        findFlag = true;
        //        idArr[i] = null;
        //    }
        //}
        //if (!findFlag)
        //    Debug.Log("未找到该项: " + obj);

        //if (idDic.ContainsValue(obj))
        //    idDic.Remove(obj);
        foreach (var pair in idDic)
        {
            if (pair.Value == obj)
                idDic.Remove(pair.Key);
        }
    }

    /// <summary>为对象获取新ID并记录</summary>
    /// <param name="obj">需要记录的对象</param>
    /// <param name="prefix">ID前缀</param>
    /// <param name="suffix">ID后缀</param>
    /// <returns>新ID</returns>
    public string GetNewID(T obj, string prefix="", string suffix="")
    {
        //for (int i = startIndex; i < maxIndex; i++)
        //{
        //    if (idArr[i] == null)
        //    {
        //        Record(i, obj);
        //        return i.ToString("D4");
        //    }
        //}
        //return null;
        for (int i = startIndex; i < maxIndex; i++)
        {
            string newID = prefix+i.ToString("D4")+suffix;
            if (!idDic.ContainsKey(newID))
            {
                Record(newID, obj);
                return newID;
            }
        }
        return null;
    }

    public T GetObj(string id)
    {
        //int index;
        //if (int.TryParse(id, out index))
        //    return GetObj(index);
        //else
        //{
        //    Debug.LogError("ID格式不正确!" + id);
        //    return null;
        //}

        if (idDic.ContainsKey(id))
            return idDic[id];
        else
            return null;
    }
    //public T GetObj(int id)
    //{
    //    if (id >= 0 && id < maxIndex)
    //        return idArr[id];
    //    else
    //        return null;
    //}

}
